package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.PersonalSalesMapper;
import com.qhg.saas_server.pojo.dto.PersonalSalesDto;
import com.qhg.saas_server.service.PersonalSalesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class PersonalSalesServiceImpl  implements PersonalSalesService {

    @Autowired
    PersonalSalesMapper personalSalesMapper;

    /**
     * 根据条件查询PersonalSales
     * @param personalSalesDto
     * @return
     */
    @Override
    public PageInfo<PersonalSalesDto> getPersonalSalesByConditon(PersonalSalesDto personalSalesDto) {

        String sortRule ="ASC";

        //判断排序方式方式，按照销售额排序
        Integer rule = personalSalesDto.getSortRule();
        if(!Objects.isNull(rule)){
            sortRule = rule == 1 ? "DESC" : "ASC";
        }

        //设置默认的serchRadius 如果没有参数则默认查询本日记录
        Integer serchRadius = personalSalesDto.getSerchRadius();
        if(Objects.isNull(serchRadius)){
            serchRadius = 1;
        }

        //开启分页插件
        PageHelper.startPage(personalSalesDto.getPageNum(),personalSalesDto.getPageSize(),"salesVolume "+sortRule);
        //查询数据
        java.util.List<PersonalSalesDto> personalSalesDtos = personalSalesMapper.listByConditon(personalSalesDto);

        PageInfo<PersonalSalesDto> pageInfo = new PageInfo<>(personalSalesDtos);
        return pageInfo;

    }
}
